﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TKD14:UIKDANSP
    {

        public TKD14()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKDANSP();
            MESSAGES += "KDDEL;";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false; 
        } 
     
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["KDSMANDANT"] = app.Mandant;
            dwData["KDSKUNDNR"] = " AND KDSKUNDNR='" + ORDERNUM.Substring(0, 8) + "'";
            dwData["KDSANSPNA"] = ART.Trim();
            dwData["KDSNAME1"] = ART.Trim();
            SResult = ((VKDANSP)MV).QueryKDANSP(dwData, 5);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
            IsSaved = false;
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            New();
            base.NewCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DKDA02(dwMaster);
            filter = "KDSANSPNA='" + dwMaster["KDSANSPNA"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 8)
                return;
            KUNDNR = EXAUF;
            dwData = TBMain.NewRow();
            dwData["KDSMANDANT"] = app.Mandant;
            dwData["KDSKUNDNR"] = EXAUF.Substring(0, 8);
            SResult = TMV.QKSP01(dwData);
            MergeData(SResult, false);
            IsSaved = false;
            base.OnEXAUFChg(vlaue);
        }
        public override void OnKUNDNRChg(string value)
        {
           

        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
            IsSaved = false;
        }
        public override void LOADBYAUF()
        {
            try
            {
                if (!IsSaved)
                    return;
                if (ORDERNUM.Length < 16)
                    return;
                if (ORDERNUM != KEY)
                    return;
                dwData = TBMain.NewRow();
                dwData["KDSMANDANT"] = app.Mandant;
                dwData["KDSKUNDNR"] =" AND KDSKUNDNR='" +ORDERNUM.Substring(0, 8)+"'"; 
                dwData["KDSANSPNA"] = ORDERNUM.Substring(8, 8); 
                SResult = ((VKDANSP)MV).QueryKDANSP(dwData, 5);
                 DataTable tb = TBMain.Clone();
                tb.Merge(SResult.Tables[0]);
                foreach (DataRow row in tb.Rows)
                {
                    dwMaster.ItemArray = row.ItemArray;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
